package com.dmagic.cms.service.impl;

import javax.persistence.RollbackException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dmagic.cms.common.exception.ValidateException;
import com.dmagic.cms.service.EmailService;

@Service
@Transactional(readOnly = true, rollbackFor = { ValidateException.class, RollbackException.class })
public class EmailServiceImpl implements EmailService {
	private static final Log log = LogFactory.getLog(EmailServiceImpl.class);

	@Autowired
	private MailSender  mailSender;
	@Autowired
	private SimpleMailMessage  simpleMailMessage;
	
	/**
     * @方法名: sendMail 
     * @参数名：@param subject  邮件主题
     * @参数名：@param content 邮件主题内容
     * @参数名：@param to         收件人Email地址
     * @描述语: 发送邮件
     */
    public void sendMail(String subject, String content, String to) {
        simpleMailMessage.setSubject(subject); //设置邮件主题
        simpleMailMessage.setTo(to);             //设定收件人
        simpleMailMessage.setText(content);  //设置邮件主题内容
        mailSender.send(simpleMailMessage); //发送邮件
    }
      //Spring 依赖注入
    public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
        this.simpleMailMessage = simpleMailMessage;
    }
      //Spring 依赖注入 
    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }
    
	/**
     * @方法名: sendMail 
     * @参数名：@param content 邮件主题内容
     * @描述语: 发送邮件
     */
    public void sendMail(String content) {
        simpleMailMessage.setText(content);  //设置邮件主题内容
        mailSender.send(simpleMailMessage); //发送邮件
    }
}
